package com.hzw.saas.web.app.user.controller;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.group.IGroupMemberReviewService;
import com.hzw.saas.api.group.IGroupMemberService;
import com.hzw.saas.api.group.IGroupService;
import com.hzw.saas.api.group.bo.GroupBo;
import com.hzw.saas.api.group.enums.GroupMemberStatus;
import com.hzw.saas.api.user.IUserAccountService;
import com.hzw.saas.api.user.bo.UserAccountBO;
import com.hzw.saas.api.user.bo.UserAccountProfileBO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.PageUtils;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.web.app.user.dto.SearUserGroupInfoDto;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiParam;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/3/3 13:57
 */

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户中心/用户检索")
@Validated
public class UserAccountSearchController {

    private final IUserAccountService userAccountService;
    private final IGroupMemberReviewService groupMemberReviewService;
    private final IGroupMemberService groupMemberService;
    private final IGroupService groupService;

    @GetMapping("/")
    @SysLog
    @ApiOperation(value = "用户搜索", notes = "根据用户")
    @ApiOperationSort(1)
    public ResponseEntity<IPage<SearUserGroupInfoDto>> searchUser(PageParam pageParam,
                                                                  @ApiParam("群组ID") @RequestParam(value = "groupId") String groupId,
                                                                  @ApiParam("真实姓名") @RequestParam(value = "realName", required = false) String realName,
                                                                  @ApiParam("邮箱")  @RequestParam(value = "email", required = false) String email,
                                                                  @ApiParam("手机号码") @RequestParam(value = "phoneNo", required = false) String phoneNo
                                                            ) {
        UserAccountBO userAccountBO = new UserAccountBO();
        userAccountBO.setEmail(email);
        userAccountBO.setPhoneNo(phoneNo);
        userAccountBO.setUserName(realName);
        AssertUtil.assertThrow("用户检索失败,必须指定一个检索条件",
            StringUtil.isBlank(userAccountBO.getUserName()) &&
            StringUtil.isBlank(userAccountBO.getPhoneNo()) &&
            StringUtil.isBlank(userAccountBO.getEmail()));
        AssertUtil.assertThrow("用户检索失败,必须指定群组ID", StringUtil.isBlank(groupId));
        GroupBo groupBo = groupService.getGroupById(groupId);
        AssertUtil.assertThrow("用户检索失败,必须指定正确的群组ID",
            Objects.isNull(groupBo) ||  //groupId 是否存在
            !Objects.equals(SecurityUtils.getUser().getUserId(), groupBo.getOwnerId())); // groupId是否为自己
        // 查询当前群组下所有成员ID
        List<String> userIds = groupMemberService.getAllMemberUserIdByGroupId(groupId);
        IPage<UserAccountProfileBO> userAccountProfileDtoIPage = userAccountService.pageAccountsByGroup(pageParam, userAccountBO, userIds);
        IPage<SearUserGroupInfoDto> searUserGroupInfoDtoIPage = PageUtils.convert(userAccountProfileDtoIPage, SearUserGroupInfoDto.class);
        searUserGroupInfoDtoIPage.getRecords().forEach(userGroupInfo->{
            GroupMemberStatus groupMemberStatus = groupMemberReviewService.getGroupMemberStatus(userGroupInfo.getUserId(), groupId, 1);
            userGroupInfo.setApplyStatus(groupMemberStatus.value());
            userGroupInfo.setApplyText(groupMemberStatus.text());
        });
        return ResponseEntity.ok(searUserGroupInfoDtoIPage);
    }

}
